<?php
$post=json_decode($this->input->raw_input_stream,true);

function fullpath($url)
{
    if(substr($url,0,2)=="//")
    {
        return "http:".$url;
    }
    else
    {
        return $url;
    }
}

$gid=$post["id"];
if($gid==0)
{
    $good=$this->goods_model->search_good($post["name"],$shop["id"]);
    if($good)
    {
        die("exist");
    }
}
else
{
    if($this->goods_model->getbytbid($gid,$shop["id"])) die("exist");
}

$product=array(
    "catid"=>$shop["id"],
    "tbid"=>$gid,
);
if(isset($post["desc"]) && $post["desc"])
$product["desc"] = $post["desc"];
else
$product["desc"] ='';
if(isset($post["thumb"]) && $post["thumb"])
$product["thumb"]= $post["img"];
else
$product["thumb"]=[];
if(isset($post["tags"]) && $post["tags"])
$product["tags"]= $post["tags"];

$product["thumb"]=serialize($product["thumb"]);        
$product["name"]=$post['name'];
$product["price"]=sprintf("%.2f",$post["price"]);

$prop=array();

if(isset($post["option"]) && $post["option"]){
    foreach($post["option"] as $op)
    {
        $v1=array("name"=>$op["name"],"value"=>$op["value"]);
        if(isset($op["image"]) && $op["image"])
        {
            $v1["image"]=$op["image"];
        }
        if(isset($op["price"]) && $op["price"])
        {
            $v1["price"]=$op["price"];
        }
        $prop["vv"][]=$v1;
    }
}

$product["name"]=ucwords(strtolower($product["name"]));


$product["option"]=$prop;
$product["source"]="shopifyorder";
if(isset($post["url"]) && $post["url"])
$product["source_url"]=$post["url"];
else
$product["source_url"]="#";
$product["option"]=serialize($prop);
$product["meta"]=$product["name"];
$newproduct=$this->goods_model->create($product);
if($newproduct){
    die("ok");
}else{
    die("error");
}